package com.hp.controller;

import com.hp.annotation.Power;
import com.hp.entity.Video;
import com.hp.process.KeepAlive;
import com.hp.process.Server;
import com.hp.result.DataGridResultInfo;
import com.hp.result.SubmitResultInfo;
import com.hp.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@Power(name = "设备管理",type =0)
@Controller
@RequestMapping("/product")
public class ProductAction {
    @Autowired
    private Server server;
    @Autowired
    private VideoService videoService;

    /**
     * 去设备管理界面
     *
     * @return
     */
    @Power(name = "去设备管理界面",type =1,url = "/product/queryproduct")
    @GetMapping("/queryproduct")
    public String queryproduct() {

        return "product/querylproducts";
    }

    @Power(name = "设备查询",type =1,url = "/product/queryproduct_result")
    @RequestMapping("/queryproduct_result")
    @ResponseBody
    public DataGridResultInfo queryproduct_result(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows) throws Exception {

        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        List<KeepAlive> list = new ArrayList<>();
        ConcurrentHashMap.KeySetView<String, KeepAlive> strings = Server.datalist.keySet();
        for (String string : strings) {
            KeepAlive keepAlive = Server.datalist.get(string);
            list.add(keepAlive);
        }
        dataGridResultInfo.setRows(list);
        dataGridResultInfo.setTotal(list.size());//总数
        return dataGridResultInfo;

    }

    /**
     * 去命令指定页面
     *
     * @return
     */
    @Power(name = "去命令指定页面",type =1,url = "/product/toSendTask")
    @GetMapping("/toSendTask")
    public String toSendTask(String id, Model model) throws Exception {
        //拿到当前keepAlive
        KeepAlive keepAlive = server.getDatalist().get(id);
        model.addAttribute("keepAlive", keepAlive);

        List list = videoService.page(1, Integer.MAX_VALUE, new Video()).getRows();
        model.addAttribute("list", list);

        return "product/sendTask";

    }

    /**
     * 发送数据
     *
     * @return
     */
    @Power(name = "命令指定发送",type =1,url = "/product/sendSubmit")
    @ResponseBody
    @PostMapping("/sendSubmit")
    public SubmitResultInfo sendSubmit(String kid,String id) throws Exception {

        return videoService.sendSubmit(kid,id);
    }
}
